package com.mes.cloud.service.productpricegroup;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mes.cloud.api.dto.request.productpricegroup.*;
import com.mes.cloud.api.dto.response.productpricegroup.ProductPriceGroupDto;
import com.mes.cloud.exception.BaseException;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.ProductPriceGroupDao;
import com.mes.cloud.dao.ProductPriceGroupProductDao;
import com.mes.cloud.entity.ProductPriceGroup;
import com.mes.cloud.entity.ProductPriceGroupProduct;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.service.IPKeyGenerator;
import com.mes.cloud.service.SysCodeGeneratorService;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 产品价格组
 *
 * @author wxm
 * @version 2019-09-24 16:08:41
 * @email xiaomin.wu1@pactera.com
 */
@Service
public class ProductPriceGroupService {

    private ProductPriceGroupDao productPriceGroupDao;

    private ProductPriceGroupProductDao productPriceGroupProductDao;

    private SysCodeGeneratorService sysCodeGeneratorService;

    private IPKeyGenerator iPKeyGenerator;

    @Autowired
    public ProductPriceGroupService(ProductPriceGroupDao productPriceGroupDao, ProductPriceGroupProductDao productPriceGroupProductDao, SysCodeGeneratorService sysCodeGeneratorService, IPKeyGenerator iPKeyGenerator) {
        this.productPriceGroupDao = productPriceGroupDao;
        this.productPriceGroupProductDao = productPriceGroupProductDao;
        this.sysCodeGeneratorService = sysCodeGeneratorService;
        this.iPKeyGenerator = iPKeyGenerator;
    }

    /**
     * 分页查列表
     *
     * @param productPriceGroupPageListRequest
     * @return JsonResult
     */
    public JsonResult<CommPager<ProductPriceGroupDto>> pageList(ProductPriceGroupPageListRequest productPriceGroupPageListRequest) {
        Page<ProductPriceGroupDto> page = PageHelper.startPage(productPriceGroupPageListRequest.getCurrentPage(), productPriceGroupPageListRequest.getPageSize());
        //查询条件
        ProductPriceGroupListRequest productPriceGroupListRequest = new ProductPriceGroupListRequest();
        BeanUtils.copyProperties(productPriceGroupPageListRequest, productPriceGroupListRequest);
        List<ProductPriceGroup> productPriceGroupList = productPriceGroupDao.list(productPriceGroupListRequest);
        List<ProductPriceGroupDto> productPriceGroupDtoList = new ArrayList<>();
        for (ProductPriceGroup productPriceGroup : productPriceGroupList) {
            ProductPriceGroupDto productPriceGroupDto = new ProductPriceGroupDto();
            BeanUtils.copyProperties(productPriceGroup, productPriceGroupDto);
            productPriceGroupDtoList.add(productPriceGroupDto);
        }
        CommPager<ProductPriceGroupDto> pageResult = new CommPager<>(page, productPriceGroupDtoList);
        JsonResult<CommPager<ProductPriceGroupDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageResult);
        return jsonResult;
    }

    /**
     * 新增
     *
     * @param productPriceGroupAddRequest
     * @return JsonResult
     */
    public JsonResult add(ProductPriceGroupAddRequest productPriceGroupAddRequest) {
        JsonResult jsonResult = new JsonResult();
        ProductPriceGroup productPriceGroup = new ProductPriceGroup();
        BeanUtils.copyProperties(productPriceGroupAddRequest, productPriceGroup);
        productPriceGroup.setCode(Constants.PRODUCT_PRICE_GROUP_PREFIX+sysCodeGeneratorService.getCode(Constants.CODE_GENERATOR_PRODUCT_PRICE_GROUP));
        productPriceGroup.setId(iPKeyGenerator.getGenerateKey());
        productPriceGroup.setUpdateDateStr(DateUtil.updateDateStr());
        productPriceGroup.setSource(Constants.SOURCE_CRM);
        productPriceGroup.setCreateBy(UserUtil.getCreateByName());
        productPriceGroup.setUpdateBy(UserUtil.getUpdateByName());
        productPriceGroup.setCreateByNo(UserUtil.getCreateByNo());
        productPriceGroup.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = productPriceGroupDao.insertSelective(productPriceGroup);
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 更新
     *
     * @param productPriceGroupUpdateRequest
     * @return JsonResult
     */
    public JsonResult update(ProductPriceGroupUpdateRequest productPriceGroupUpdateRequest) {
        ProductPriceGroup productPriceGroup = new ProductPriceGroup();
        BeanUtils.copyProperties(productPriceGroupUpdateRequest, productPriceGroup);
        productPriceGroup.setUpdateDateStr(DateUtil.updateDateStr());
        productPriceGroup.setUpdateBy(UserUtil.getUpdateByName());
        productPriceGroup.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = productPriceGroupDao.updateByPrimaryKeySelective(productPriceGroup);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 查看详情
     *
     * @param id
     * @return JsonResult
     */
    public JsonResult<ProductPriceGroupDto> get(String id) {
        JsonResult jsonResult = new JsonResult();
        ProductPriceGroup productPriceGroup = productPriceGroupDao.selectByPrimaryKey(id);
        if (productPriceGroup == null) {
            return jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode()).setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
        }
        //统计价格组的客户数
        productPriceGroup.setCustomerCount(productPriceGroupDao.productPriceGroupCountCustomer(id));
        //统计价格组的产品数
        ProductPriceGroupProduct productPriceGroupProductParam = new ProductPriceGroupProduct();
        productPriceGroupProductParam.setProductPriceGroupId(id);
        productPriceGroup.setProductCount(productPriceGroupProductDao.selectCount(productPriceGroupProductParam));
        ProductPriceGroupDto productPriceGroupDto = new ProductPriceGroupDto();
        BeanUtils.copyProperties(productPriceGroup, productPriceGroupDto);
        return jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(productPriceGroupDto);
    }

    /**
     * 禁用启用
     *
     * @param id
     * @param enableFlag
     * @return JsonResult
     */
    public JsonResult updateEnableFlag(String id, String enableFlag) {
        ProductPriceGroup productPriceGroup = new ProductPriceGroup();
        productPriceGroup.setId(id);
        productPriceGroup.setEnableFlag(enableFlag);
        int count = productPriceGroupDao.updateByPrimaryKeySelective(productPriceGroup);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 产品价格组批量启用禁用
     * @param productPriceGroupBatchEnableFlagRequest
     * @return
     */
    public JsonResult batchEnableFlag(ProductPriceGroupBatchEnableFlagRequest productPriceGroupBatchEnableFlagRequest) {
        JsonResult jsonResult = new JsonResult();
        productPriceGroupDao.batchEnableFlag(productPriceGroupBatchEnableFlagRequest.getEnableFlag(),
                productPriceGroupBatchEnableFlagRequest.getIds(),
                DateUtil.updateDateStr(), UserUtil.getEmployeeNo(), UserUtil.getName());
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
        jsonResult.setCode(HttpStatusEnum.OK.getCode());
        return jsonResult;
    }
}
